package com.kk.base.controller.home;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.Result;
import com.kk.base.constant.SessionConstant;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.service.home.HomeUserService;
import com.kk.base.util.SessionUtil;
import com.kk.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 前台登录注册控制层
 */
@Controller
@RequestMapping("/home/index")
public class HomeLoginController {

    @Autowired
    private HomeUserService homeUserService;

    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping(value="/login",method= RequestMethod.GET)
    public String login(){
        return "home/login";
    }

    /**
     * 用户登录
     * @param mobile
     * @param password
     * @return
     */
    @RequestMapping(value="/login",method= RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> login(@RequestParam("mobile")String mobile,@RequestParam("password")String password) {
        //正则验证输入的手机号是否合法
        String checkPhone = "^1[3|4|5|7|8]\\d{9}$";
        Pattern r = Pattern.compile(checkPhone);
        Matcher m = r.matcher(mobile);
        if (!m.matches()) {
            return Result.error(CodeMsg.HOMEUSER_MOBILE_ERROR);
        }
        HomeUser byMobile = homeUserService.findByMobile(mobile);
        if (byMobile == null){
            return Result.error(CodeMsg.HOMEUSER_NO_EXIST);
        }
        if (byMobile.getStatus() == 0){
            return Result.error(CodeMsg.HOMEUSER_STATUS_ERROR);
        }
        if (!byMobile.getPassword().equals(password)){
            return Result.error(CodeMsg.HOMEUSER_PASSWORD_ERROR);
        }
        SessionUtil.set(SessionConstant.SESSION_HOME_USER_LOGIN_KEY, byMobile);
        return Result.success(true);

    }


    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping(value="/register",method= RequestMethod.GET)
    public String register(){
        return "home/register";
    }

    /**
     * 用户注册
     * @param homeUser
     * @param code
     * @return
     */
    @RequestMapping(value="/register",method= RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> register(HomeUser homeUser, @RequestParam("reCode") String code) {
        //正则验证输入的手机号是否合法
        String checkPhone = "^1[3|4|5|7|8]\\d{9}$";
        Pattern r = Pattern.compile(checkPhone);
        Matcher m = r.matcher(homeUser.getMobile());
        if (!m.matches()) {
            return Result.error(CodeMsg.HOMEUSER_MOBILE_ERROR);
        }
        if (!StringUtil.emailFormat(homeUser.getEmail())) {
            return Result.error(CodeMsg.COMMON_EMAIL_FORMAET_ERROR);
        }
        if (code == null) {
            return Result.error(CodeMsg.HOMEUSER_CODE_ERROR);
        }
        if (code.length() != 4) {
            return Result.error(CodeMsg.HOMEUSER_CODE_LENGTH_ERROR);
        }
        Object attr = SessionUtil.get(SessionConstant.HOME_USER_REGISTER_CODE);
        if (attr == null) {
            return Result.error(CodeMsg.CODE_NOT_EMPTY);
        }
        if (!code.equalsIgnoreCase(attr.toString())) {
            return Result.error(CodeMsg.CPACHA_ERROR);
        }
        //判断手机号是否已注册
        if(homeUserService.isExistMobile(homeUser.getMobile(),0L)){
            return Result.error(CodeMsg.HOMEUSER_MOBILE_EXIST);
        }
        if (homeUserService.save(homeUser) == null) {
            return Result.error(CodeMsg.HOMEUSER_ADD_ERROR);
        }
        SessionUtil.set(SessionConstant.HOME_USER_REGISTER_CODE, null);
        return Result.success(true);

    }

    /**
     * 跳转到忘记密码
     * @return
     */
    @RequestMapping(value="/forget",method= RequestMethod.GET)
    public String forget(){
        return "home/forget_password";
    }

    /**
     * 修改密码
     * @param homeUser
     * @param code
     * @return
     */
    @RequestMapping(value="/forget",method= RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> forget(HomeUser homeUser, @RequestParam("reCode") String code) {
        //正则验证输入的手机号是否合法
        String checkPhone = "^1[3|4|5|7|8]\\d{9}$";
        Pattern r = Pattern.compile(checkPhone);
        Matcher m = r.matcher(homeUser.getMobile());
        if (!m.matches()) {
            return Result.error(CodeMsg.HOMEUSER_MOBILE_ERROR);
        }
        if (!StringUtil.emailFormat(homeUser.getEmail())) {
            return Result.error(CodeMsg.COMMON_EMAIL_FORMAET_ERROR);
        }
        if (code == null) {
            return Result.error(CodeMsg.HOMEUSER_CODE_ERROR);
        }
        if (code.length() != 4) {
            return Result.error(CodeMsg.HOMEUSER_CODE_LENGTH_ERROR);
        }
        Object attr = SessionUtil.get(SessionConstant.HOME_USER_FORGET_PASSWORD_CODE);
        if (attr == null) {
            return Result.error(CodeMsg.CODE_NOT_EMPTY);
        }
        if (!code.equalsIgnoreCase(attr.toString())) {
            return Result.error(CodeMsg.CPACHA_ERROR);
        }
        //判断手机号是否已注册
        HomeUser byMobile = homeUserService.findByMobile(homeUser.getMobile());
        if (byMobile == null) {
            return Result.error(CodeMsg.HOMEUSER_NO_EXIST);
        }
        if (!byMobile.getEmail().equals(homeUser.getEmail())){
            return Result.error(CodeMsg.CODE_NOT_ERROR);
        }
        byMobile.setPassword(homeUser.getPassword());
        if (homeUserService.save(byMobile) == null) {
            return Result.error(CodeMsg.HOMEUSER_FORGET_ERROR);
        }
        SessionUtil.set(SessionConstant.HOME_USER_FORGET_PASSWORD_CODE, null);
        return Result.success(true);

    }

    /**
     * 退出
     * @return
     */
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> logout() {
        HttpSession session = SessionUtil.getSession();
        HomeUser homeUser = SessionUtil.getHomeUser();
        if (homeUser != null) {
            session.removeAttribute(SessionConstant.SESSION_HOME_USER_LOGIN_KEY);
        }
        return Result.success(true);
    }


}
